package com.ybear.ybcomponent;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.PopupWindow;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.ybear.ybcomponent.base.adapter.BaseRecyclerViewAdapter;

import java.util.Collections;
import java.util.List;

/**
 * 工具类
 */
public class Utils {
    /**
     * px转dp
     * @param context       上下文
     * @param px            像素值
     * @return              返回的dp值
     */
    public static float px2Dp(@Nullable Context context, float px) {
        return context == null ? px : px / context.getResources().getDisplayMetrics().density + 0.5f;
    }
    public static int px2Dp(@Nullable Context context, int px) {
        return (int) px2Dp(context, (float) px);
    }

    /**
     * dp转px
     * @param context       上下文
     * @param dp            dip值
     * @return              返回的px值
     */
    public static float dp2Px(@Nullable Context context, float dp) {
        return context == null ? dp : dp * context.getResources().getDisplayMetrics().density + 0.5f;
    }
    public static int dp2Px(@Nullable Context context, int dp) {
        return (int) dp2Px(context, (float) dp);
    }

    /**
     * 优化版Touch
     * @param view      优化的控件
     * @param l         监听器
     */
    public static void setOnSuperTouchListener(@NonNull View view, OnTouchListener l) {
        view.setTag( R.id.touch_down_time, -1L );
        view.setOnTouchListener((v, event) -> {
            boolean ret = l != null && l.onTouch(v, event);
            long downTime = (long) view.getTag( R.id.touch_down_time );
            switch ( event.getAction() ) {
                case MotionEvent.ACTION_DOWN:
                    downTime = System.currentTimeMillis();
                    break;
                case MotionEvent.ACTION_UP:
                    if( !ret ) break;
                    long upTime = System.currentTimeMillis();
                    //不是长按时才触发优化，防止长按后抬起时触发一次点击事件
                    if( upTime - downTime < ViewConfiguration.getLongPressTimeout() ) {
                        v.performClick();
                    }
                    downTime = -1;
                    break;
            }
            view.setTag( R.id.touch_down_time, downTime );
            return ret;
        });
    }

    /**
     * 控件转图片
     * @param v     控件
     * @return      图片
     */
    public static Bitmap viewToBitmap(@NonNull View v, int width, int height) {
        if( width <= 0 || height <= 0 ) return null;
        Bitmap bmp = Bitmap.createBitmap( width, height, Bitmap.Config.ARGB_8888 );
        v.draw( new Canvas( bmp ) );
        return bmp;
    }
    public static Bitmap viewToBitmap(@NonNull View v) {
        if( v.getWidth() == 0 || v.getHeight() == 0 ) {
            v.measure( 0, 0 );
        }
        return viewToBitmap( v, v.getWidth(), v.getHeight() );
    }

    /**
     * 创建一个Popup弹窗
     * @param v     需要弹窗的布局
     * @return      Popup
     */
    public static PopupWindow createPopupWindow(@NonNull View v) {
        PopupWindow pw = new PopupWindow( v );
        int w, h;
        pw.setOutsideTouchable( true );
        if( ( w = v.getWidth() ) == 0 || ( h = v.getHeight() ) == 0 ) {
            v.measure( 0, 0 );
            w = v.getMeasuredWidth();
            h = v.getMeasuredHeight();
        }
        pw.setWidth( w );
        pw.setHeight( h );
        return pw;
    }

    public void moveRecyclerViewItem(@NonNull BaseRecyclerViewAdapter adapter, int from, int to) {
        List<?> list = adapter.getDataList();
        if( from < 0 || from >= list.size() || to < 0 || to >= list.size() ) return;

        //数据排序
        if( from < to ) {
            for(int i = from; i < to; i++) Collections.swap( list, i, i + 1 );
        }else {
            for(int i = from; i > to; i--) Collections.swap( list, i, i - 1 );
        }
        //更新排序
        adapter.notifyItemMoved( from, to );
    }
}
